package com.huahang.fashionpress.controller;

import com.huahang.fashionpress.model.Category;
import com.huahang.fashionpress.service.impl.CategoryServicelImpl;
import com.huahang.fashionpress.service.impl.IProductServiceImpl;
import com.huahang.fashionpress.vo.ProductClientVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private IProductServiceImpl productService;
    @Autowired
    private CategoryServicelImpl categoryService;

    @RequestMapping("/index")
    public ModelAndView getIndexView(Map map, Integer currIndex){
        //检查商品页号是否为空 是则将其设为0
        if(currIndex == null || currIndex <= 0){
            currIndex = 0;
        }else{
            currIndex = currIndex - 1;
        }
        //获取商品列表
        List<ProductClientVO> productList = productService.getAllProduct(currIndex, 15);
        //获取商品种类列表
        List<Category> categoryList = categoryService.SelectallCategory();

//        System.out.println("aaa");
        map.put("productList", productList);
        map.put("categoryList", categoryList);
        map.put("currIndex", currIndex + 1);

        return new ModelAndView("client/index", map);
    }

    @RequestMapping("/productSearch")
    public ModelAndView getProductSearchView(Map map, Integer categoryID, Integer currIndex, Integer pageSize){
        //检查商品页号是否为空 是则将其设为0
        if(currIndex == null || currIndex <= 0){
            currIndex = 0;
            pageSize = 15;
        }else{
            currIndex = currIndex - 1;
            pageSize = 15;
        }
        //获取商品页面分页
        List<ProductClientVO> productClientVOList = productService.getKindOfProduct(categoryID, currIndex, pageSize);
        //获取商品种类列表
        List<Category> categoryList = categoryService.SelectallCategory();
        //存入商品页面
        map.put("productList", productClientVOList);
        map.put("categoryList", categoryList);
        map.put("currIndex", currIndex + 1);

        return new ModelAndView("client/productSearch", map);
    }
}
